/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.swing;

import java.util.*;

import java.awt.event.*;
import javax.swing.*;

/**
 *
 *
 * @author Francisco Morero Peyrona
 */

public class JxToolBar extends JToolBar
{
   /**
    * Zero argument constructor
    */
   public JxToolBar()
   {
      this( null );
   }

   /**
    * Constructor
    * @param as
    */
   public JxToolBar( JActionSet as )
   {
      setBorderPainted( true );
      setRollover( true );

      if( as != null )
         add( as );
   }
   
   /**
    * Add all Actions passed creating a new button per each action.<br>
    * The returned reference to the button can be used to do more with it (e.g. disabling it)
    */
   public void add( JActionSet as )
   {
      for( Enumeration e = as.actions(); e.hasMoreElements(); )
      {
         Object action = e.nextElement();

         if( action == JActionSet.SEPARATOR )
            addSeparator();
         else
            add( new TbButton( (AbstractAction) action ) );
      }
   }

   public JButton add( AbstractAction act )
   {
      TbButton btn = new TbButton( act );

      add( btn );

      return btn;
   }

   /**
    * Creates a button and return the reference.
    * The returned reference can be used to do more with it (e.g. disabling it)
    *
    * @param icon
    * @param sToolTip
    * @return
    */
   public JButton add( ImageIcon icon, String sToolTip )
   {
      return add( icon, sToolTip, null, null );
   }

   /**
    * Creates a button and return the reference.
    * The returned reference can be used to do more with it (e.g. disabling it)
    *
    * @param icon
    * @param sToolTip
    * @return
    */
   public JButton add( ImageIcon icon, String sToolTip, String sAction, ActionListener list )
   {
      TbButton btn = new TbButton( icon, sToolTip );

      if( list != null )
      {
         btn.addActionListener( list );
         btn.setActionCommand( sAction );
      }

      add( btn );

      return btn;
   }

   //-------------------------------------------------------------------------//
   // INNERCLASS: Special ToolBar Button
   //-------------------------------------------------------------------------//

   final class TbButton extends JButton
   {
      TbButton( AbstractAction act )
      {
         super( act );
         setCommon();
      }

      TbButton( ImageIcon icon, String sToolTip )
      {
         super( icon );

         setToolTipText( sToolTip );
         setCommon();
      }

      private void setCommon()
      {
         setText( null );
         setFocusPainted( false );
      }
   }
}